<template>
  <el-dialog v-model="dialogVisible" title="添加相关内容" width="30%">
    <el-form :model="dataForm" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="项目名称">
        <span>{{ dataForm.name }}</span>
      </el-form-item>
      <el-form-item label="项目类型" prop="type">
        <el-select v-model="state.type" placeholder="请选择" multiple collapse-tags collapse-tags-tooltip :max-collapse-tags="3">
          <el-option v-for="item in state.projectType" :key="item.id" :value="item.id" :label="item.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目标签" prop="label">
        <el-select v-model="state.label" placeholder="请选择" multiple collapse-tags collapse-tags-tooltip :max-collapse-tags="3">
          <el-option v-for="item in state.projectLabel" :key="item.id" :value="item.id" :label="item.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否有代码" prop="codeStatus">
        <!--        <el-input v-model="dataForm.codeStatus" placeholder="是否有代码(0：有1：没有)"></el-input>-->
        <el-switch v-model="dataForm.codeActive" inline-prompt/>
        <el-button type="primary" plain v-show="dataForm.codeActive" @click="checkCode(dataForm.id)" style="margin-left: 5%">点击添加源代码</el-button>
      </el-form-item>
      <el-form-item label="是否有论文" prop="pageStatus">
        <!--        <el-input v-model="dataForm.pageStatus" placeholder="是否有论文(0：有1：没有)"></el-input>-->
        <el-switch v-model="dataForm.pageActive" inline-prompt/>
        <el-button type="primary" plain v-show="dataForm.pageActive" @click="checkPage(dataForm.id)" style="margin-left: 5%">点击添加论文</el-button>
      </el-form-item>
      <el-form-item label="是否有数据集" prop="datasetStatus">
        <!--        <el-input v-model="dataForm.datasetStatus" placeholder="是否有数据集(0：有1：没有)"></el-input>-->
        <el-switch v-model="dataForm.datasetActive" inline-prompt/>
        <el-button type="primary" plain v-show="dataForm.datasetActive" @click="checkDataSet(dataForm.id)" style="margin-left: 5%">点击添加数据集</el-button>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="beforeSubmit()">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
  <Page ref="PageRef" @refreshDataList="state.getDataList" :projectId="dialogData.currentId" :projectStatus="state.currentStatus" @getPageId="getPageId"></Page>
  <Code ref="CodeRef" @refreshDataList="state.getDataList" :projectId="dialogData.currentId" :projectStatus="state.currentStatus" @getCodeId="getCodeId"></Code>
  <DataSet ref="DataSetRef" @refreshDataList="state.getDataList" :projectId="dialogData.currentId" :projectStatus="state.currentStatus" @getDataSetId="getDataSetId"></DataSet>
</template>

<script lang="ts" setup>
import {computed, nextTick, reactive, ref, toRefs} from "vue";
import { IObject } from "@/types/interface";
import baseService from "@/service/baseService";
import { useAppStore } from "@/store";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";
import useView from "@/hooks/useView";
import Page from "@/views/poap/poappage.vue"
import Code from "@/views/poap/poapsourcecode.vue"
import DataSet from "@/views/poap/poapdataset.vue"

const { t } = useI18n();
const router = useRouter();

const dialogVisible = ref(false)
const props = defineProps({
  projectId:String
})
const dataFormRef = ref();
const dataForm = reactive({
  id: "",
  name: "",
  info: "",
  status: "",
  auditorId: "",
  viewNumber: "",
  downloadNumber: "",
  codeStatus: 1,
  codeActive:false,
  pageStatus: 1,
  pageActive:false,
  datasetStatus: 1,
  datasetActive:false,
  licenseId: "",
  licenseName:"",
  delFlag: "",
  createDate: "",
  creator: "",
  updateDate: "",
  updater: "",
  pageId:[],
  codeId:[],
  dataSetId:[]
});

const dialogData = reactive({
  currentId:"",//当前项目id
  currentStatus:0,//当前项目状态
  drawerPage:false,//论文抽屉
  drawerCode:false,//源代码抽屉
  drawerDataSet:false,//数据集抽屉
});

const view = reactive({
  dataForm: {
    name: "",
    projectId:"",
  },
  currentStatus:0,
  type:[] as any[],
  label:[] as any[],
  projectLabel:[] as any[],
  projectType:[] as any[],

});

const state = reactive({ ...useView(view), ...toRefs(view) });

const store = useAppStore();
const user = computed(() => store.state.user);

const init = (id:number) => {
  dialogVisible.value = true;
  baseService.get("/poap/poapproject/" + id).then((res) => {
    console.log(res)
    Object.assign(dataForm, res.data);
    if(dataForm.pageStatus===0) dataForm.pageActive=true
    if(dataForm.codeStatus===0) dataForm.codeActive=true
    if(dataForm.datasetStatus===0) dataForm.datasetActive=true
  });
  baseService.get("/poap/poapproject/selectAllLabels").then((res)=>{
    console.log("Label")
    console.log(res)
    if(res.code != 0) return false
    state.projectLabel = res.data
    console.log(state.projectLabel)
  })
  baseService.get("/poap/poapproject/selectAllTypes").then((res)=>{
    console.log("Type")
    console.log(res)
    if(res.code != 0) return false
    state.projectType = res.data
  })
};

const PageRef = ref()
const checkPage = (id:string)=>{
  dataForm.pageId=[]
  dialogData.drawerPage = true;
  dialogData.currentId = "";
  dialogData.currentId = id;
  nextTick(()=>{
    PageRef.value.initPage();
    PageRef.value.$forceUpdate()
  })
}
const getPageId = (val:any)=>{
  dataForm.pageId=val;
}

const CodeRef = ref()
const checkCode = (id:string)=>{
  dataForm.codeId=[]
  dialogData.drawerCode = true;
  dialogData.currentId = "";
  dialogData.currentId = id;
  nextTick(()=>{
    CodeRef.value.initCode();
    CodeRef.value.$forceUpdate()
  })
}
const getCodeId = (val:any)=>{
  dataForm.codeId=val
}

const DataSetRef = ref()
const checkDataSet = (id:string)=>{
  dataForm.dataSetId=[]
  dialogData.drawerDataSet = true;
  dialogData.currentId = "";
  dialogData.currentId = id;
  nextTick(()=>{
    DataSetRef.value.initDataSet();
    DataSetRef.value.$forceUpdate()
  })
}
const getDataSetId = (val:any)=>{
  dataForm.dataSetId=val
}

const emit = defineEmits(["getDataListChild"]);

const beforeSubmit = () => {
  if(!dataForm.pageActive || !dataForm.codeActive || !dataForm.datasetActive) {
    var str = "";
    if(!dataForm.pageActive) str += `"论文",`;
    if(!dataForm.codeActive) str += `"源代码",`;
    if(!dataForm.datasetActive) str += `"数据集",`;

    str=str.slice(0,str.length-1)
    console.log(str)
    ElMessageBox.confirm(`确认不添加${str}吗，原有数据将会丢失`,"",{
      confirmButtonText:"确认",
      cancelButtonText:"取消"
    }).then(()=>{
        dataFormSubmitHandle();
    })
  }
  else dataFormSubmitHandle()
}
// 表单提交
const dataFormSubmitHandle = () => {
  if(dataForm.codeActive) dataForm.codeStatus=0;
  else dataForm.codeStatus=1;
  if(dataForm.pageActive) dataForm.pageStatus=0;
  else dataForm.pageStatus=1;
  if(dataForm.datasetActive) dataForm.datasetStatus=0;
  else dataForm.datasetStatus=1;

  console.log(dataForm.pageStatus + " " + dataForm.codeStatus + " " + dataForm.datasetStatus)
  let flag=false;
  (!dataForm.id ? baseService.post : baseService.put)("/poap/poapproject", dataForm).then((res) => {
    if(res.code!=0) return false
  });
  if(dataForm.pageActive) {
    if(dataForm.pageId === []) ElMessage.error("请添加论文");
    console.log(dataForm.pageId);
    baseService.post("/poap/poapprojectpage", {pageIds:dataForm.pageId,projectId:props.projectId}).then((res: any)=>{
      console.log(res)
      if(res.code!=0) return ElMessage.error("论文添加失败")
      flag=true;
      emit("getDataListChild");
    })
  }else {
    dataForm.pageId=[];
    baseService.post("/poap/poapprojectpage", {pageIds:dataForm.pageId,projectId:props.projectId}).then((res: any)=>{
      console.log(res)
      if(res.code!=0) return ElMessage.error("论文添加失败")
      flag=true;
      emit("getDataListChild");
    })
  }
  if(dataForm.codeActive) {
    flag=false
    if(dataForm.codeId === []) ElMessage.error("请添加代码");
    console.log(dataForm.codeId);
    baseService.post("/poap/poapprojectcode", {codeIds:dataForm.codeId,projectId:props.projectId}).then((res: any)=>{
      console.log(res)
      if(res.code!=0) return ElMessage.error("代码添加失败")
      flag=true;
      emit("getDataListChild");
    })
  }else {
    dataForm.codeId = [];
    console.log(dataForm.codeId);
    baseService.post("/poap/poapprojectcode", {codeIds:dataForm.codeId,projectId:props.projectId}).then((res: any)=>{
      console.log(res)
      if(res.code!=0) return ElMessage.error("代码添加失败")
      flag=true;
      emit("getDataListChild");
    })
  }

  if(dataForm.datasetActive) {
    flag=false
    if(dataForm.dataSetId === []) ElMessage.error("请添加数据集");
    console.log(dataForm.dataSetId);
    baseService.post("/poap/poapprojectdataset", {datasetIds:dataForm.dataSetId,projectId:props.projectId}).then((res: any)=>{
      console.log(res)
      if(res.code!=0) return ElMessage.error("数据集添加失败")
      flag=true;
      emit("getDataListChild");
    })
  } else {
    dataForm.dataSetId = [];
    console.log(dataForm.dataSetId);
    baseService.post("/poap/poapprojectdataset", {datasetIds:dataForm.dataSetId,projectId:props.projectId}).then((res: any)=>{
      console.log(res)
      if(res.code!=0) return ElMessage.error("数据集添加失败")
      flag=true;
      emit("getDataListChild");
    })
  }

  console.log(props.projectId);
  if(state.type) flag=false;
  baseService.post("/poap/poapprojecttype", {typeIds:state.type,projectId:props.projectId}).then((res: any)=>{
    console.log(res)
    if(res.code!=0) return ElMessage.error("项目类型添加失败")
    flag=true;
    emit("getDataListChild");
  });
  flag=false;
  baseService.post("/poap/poapprojectlabel", {labelIds:state.label,projectId:props.projectId}).then((res: any)=>{
    console.log(res)
    if(res.code!=0) return ElMessage.error("项目标签添加失败")
    flag=true;
    emit("getDataListChild");
  })
  // console.log(flag)
  // if(flag) {
    dialogVisible.value=false
    ElMessage.success("添加成功")

  // }
};
defineExpose({init})
</script>
